package com.imooc.article.controller;

import com.imooc.api.controller.article.CommentControllerApi;
import com.imooc.api.controller.user.BaseControllerApi;
import com.imooc.api.controller.user.UserControllerApi;
import com.imooc.article.service.CommentPortalService;
import com.imooc.grance.result.GraceJsonResult;
import com.imooc.pojo.bo.CommentReplyBO;
import com.imooc.pojo.vo.AppUserInfoVO;
import com.imooc.utils.JsonUtils;
import com.imooc.utils.PagedGridResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@RestController
public class CommentController  extends BaseControllerApi implements CommentControllerApi {
    @Autowired
    private CommentPortalService commentPortalService;
    @Override
    public GraceJsonResult createComment(@Valid CommentReplyBO commentReplyBO) {
        String commentUserId = commentReplyBO.getCommentUserId();
        Set idSet = new HashSet();
        idSet.add(commentUserId);
        List<AppUserInfoVO> userInfoVOList = getUSerInfoVOList(idSet);
        AppUserInfoVO appUserInfoVO = userInfoVOList.get(0);
        String nickname = appUserInfoVO.getNickname();
        String face = appUserInfoVO.getFace();
        commentPortalService.createComment(commentReplyBO.getArticleId(),
                                                            commentUserId,
                                                            nickname,
                                                            face,
                                                            commentReplyBO.getFatherId(),
                                                            commentReplyBO.getContent());
        return GraceJsonResult.ok();
    }

    @Override
    public GraceJsonResult counts(String articleId) {
        return GraceJsonResult.ok(commentPortalService.getCommentCounts(articleId));
    }

    @Override
    public GraceJsonResult list(String articleId,
                                Integer page,
                                Integer pageSize) {
        if (page == null) {
            page = COMMON_PAGE;
        }
        if (pageSize == null) {
            pageSize = COMMON_PAGE_SIZE;
        }
        PagedGridResult commentsList = commentPortalService.getCommentsList(articleId,page, pageSize);
        return GraceJsonResult.ok(commentsList);
    }

    @Override
    public GraceJsonResult mng(String writerId, Integer page, Integer pageSize) {
        PagedGridResult pagedGridResult = commentPortalService.commnetMng(writerId, page, pageSize);
        return GraceJsonResult.ok(pagedGridResult);
    }

    @Override
    public GraceJsonResult delete(String writerId, String commentId) {
        commentPortalService.deleteComments(writerId, commentId);
        return GraceJsonResult.ok();
    }

    @Autowired
    private UserControllerApi userController;
    public List<AppUserInfoVO> getUSerInfoVOList(Set idSet) {
        //使用feign发起调用，直接调用接口,解决硬编码拼接调用的弊端
        GraceJsonResult body = userController.queryByIds(JsonUtils.objectToJson(idSet));
//        System.out.println("body: "+body);
        //使用微服务的注册与发现远程调用方法
//        String serviceId = "SERVICE-USER";
//        //直接调用serviceId,他会自动的去注册表中为我们去寻找对应的ip和端口号,另外还要在RestTemplate上添加负载均衡的注解
//        String url =
//                "http://"+serviceId+"/user/queryByIds?userIds=" + JsonUtils.objectToJson(idSet);
        /*List<ServiceInstance> instanceList = discoveryClient.getInstances(serviceId);
        ServiceInstance userServiceInstance = instanceList.get(0);
        String host = userServiceInstance.getHost();
        int port = userServiceInstance.getPort();
        String url =
                "http://"+host+":"+port+"/user/queryByIds?userIds=" + JsonUtils.objectToJson(idSet);
*/

//        String url =
//                "http://user.imoocnews.com:8003/user/queryByIds?userIds=" + JsonUtils.objectToJson(idSet);
//        ResponseEntity<GraceJsonResult> entityResult =
//                restTemplate.getForEntity(url, GraceJsonResult.class);
//        GraceJsonResult body = entityResult.getBody();
        List<AppUserInfoVO> userInfoVOList = null;

        if (body.getStatus() == 200) {
            String userListJson = JsonUtils.objectToJson(body.getData());
            userInfoVOList = JsonUtils.jsonToList(userListJson, AppUserInfoVO.class);
//            System.out.println("userInfoVOList: "+userInfoVOList.toString());
        }else {
            userInfoVOList = new ArrayList<>();
        }
        return userInfoVOList;
    }

}
